package com.jie.listening;

import com.jie.po.EmailCodePO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

import static com.jie.constants.RabbitMQConstant.*;

/**
 * @Author: 杰
 * @Project :net-disk
 * @DateTime: 2024/5/6 12:33
 **/
@Component
@Slf4j
public class EmailListenConsumer {
    private final JavaMailSender javaMailSender;

    public EmailListenConsumer(JavaMailSender javaMailSender) {
        this.javaMailSender = javaMailSender;
    }

    /**
     * 验证码发送邮箱
     * @param emailCodePO
     */
    @RabbitListener(bindings = {
            @QueueBinding(
                    value = @Queue(value = SIGN_CODE_QUEUE, durable = "true", autoDelete = "false"),
                    exchange = @Exchange(value = EMAIL_CODE_EXCHANGE),
                    key = EMAIL_CODE_KEY
            )},containerFactory = "rabbitListenerContainerFactory")
    public void listenSendSimpleEmail(@Payload EmailCodePO emailCodePO) {
        //发送消息
        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
        simpleMailMessage.setTo(emailCodePO.getTo());
        simpleMailMessage.setText(emailCodePO.getText());
        simpleMailMessage.setFrom(emailCodePO.getFrom());
        simpleMailMessage.setSubject(emailCodePO.getSubject());
        javaMailSender.send(simpleMailMessage);
        log.info("发送邮件成功{}",emailCodePO);
    }

}
